<template>
  <div>
    <t-dialog
      theme="info"
      header="专家信息完善提示"
      body="请完善个人专家信息和电子签名后再评审项目"
      :visible.sync="visible"
      @confirm="onExpertInfoConfirm"
      :onClose="onExpertInfoClose"
      :cancelBtn="null"
    />
    <t-table rowKey="id" :data="data" :columns="columns" hover :pagination="pagination"
             :loading="dataLoading" >
      <template #sortNum="{ rowIndex }">
        {{rowIndex+1}}
      </template>
      <template #reviewTeamName="{ row }">
        <t-link @click="handleReviewTeamDetail(row)" hover="color" theme="primary">{{ row?.reviewTeam?.name }}</t-link>
      </template>
      <template #reviewTeamType="{ row }">
        {{ row?.reviewTeam?.reviewType | filterByDict(dict.type.cgzh_review_type) }}
      </template>
      <template #[`collectResult.name`]="{ row }">
        <t-link @click="handleProjectDetail(row)" hover="color" theme="primary">{{ row.collectResult.name }}</t-link>
      </template>
      <template #reviewStatus="{row}">
        <t-tag theme="success" v-if="row.reviewStatus==='1'">
          已提交
        </t-tag>
        <t-tag  v-else-if="row.reviewStatus === '0'" theme="warning" variant="light">暂存</t-tag>
        <t-tag theme="danger" v-else>
          未提交
        </t-tag>
      </template>
      <template #applyDept="{ row }">
        {{row.collectResult.sysDept.deptName}}
      </template>
      <template #applyUser="{ row }">
        {{row.collectResult.projectLeaderName}}
      </template>
      <template #op="{ row }">
        <t-button @click="openReview(row)" size="small" variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon" />
          {{isReview?'评审':'继续评审'}}
        </t-button>
        <t-button @click="handleProjectDetail(row)" size="small" variant="text" class="t-button-link">
          <t-icon name="info-circle" slot="icon"/>
          详情
        </t-button>
      </template>
    </t-table>
    <collect-detail-dialog :objId="dialog.objId" :header="dialog.header" :visible.sync="dialog.open_detail" v-if="dialog.open_detail">
    </collect-detail-dialog>
    <review-group-all-detail :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_review_group" v-if="dialog.open_review_group"></review-group-all-detail>
    <cgzh-approve-info @reload="getList(1)" :header="dialog.header" :editOpen="true" :expertEditOpen="true" :row="dialog.row" :visible.sync="dialog.open_approve_info"
                      v-if="dialog.open_approve_info"></cgzh-approve-info>
  </div>
</template>

<script>

import cgzhApproveInfo from "./cgzh-approve-info.vue";
import ReviewGroupAllDetail from "../../../cgzh/review/components/review-group-all-detail.vue";
import {getFiles} from "@/api/common";
import collectDetailDialog from "../../../cgzh/collect/components/collect-detail-dialog.vue";


export default {
  name: 'cgzh-list',
  dicts: ["cgzh_review_type"],
  components: {
    cgzhApproveInfo,
    ReviewGroupAllDetail,
    collectDetailDialog
  },
  props: {
    isReview: {
      type: Boolean,
      default: true,
    },
    visible: false
  },
  data() {
    return {
      dataLoading: false,
      data: [],
      columns: [
        { colKey: 'sortNum', title: '序号', align: 'center', width: 64 },
        { colKey: 'reviewTeamName', title: '评审组信息', align: 'center' },
        // { colKey: 'reviewTeamType', title: '评审类型', align: 'center' },
        { colKey: 'collectResult.name', title: '成果名称', align: 'center' },
        {colKey: 'reviewStatus', title: '评审状态',align: "center"},
        {colKey: 'sort', title: '在本组中的排序',align: "center"},
        // { colKey: 'applyDept', title: '申报单位', align: 'center' },
        // { colKey: 'applyUser', title: '申报人', align: 'center' },
        { colKey: 'reviewTeam.startReviewDate', title: '评审开始时间', align: 'center' },
        { colKey: 'reviewTeam.endReviewDate', title: '评审截止时间', align: 'center' },
        { colKey: 'reviewTeam.reviewMeetingDate', title: '评审会议时间', align: 'center' },
        { colKey: 'op', title: '操作', align: 'center' },
      ],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10,20,30],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      dialog: {
        open_review: false,
        open_project_detail: false,
        open_review_standard: false,
        header: '',
        objId: 0,
        teamId: 0,
        reviewType: '',
        open_approve_info: false,
        row: {},
        open_review_group: false,
        open_detail: false,
      },
      expertInfo: {},
      refreshStart: false,
      signatureFiles: []
    };
  },
  async created() {
    await this.$api.zjk.expert.getExpertsInfo().then(res => {
      if(res.data){
        this.expertInfo = res.data;
        getFiles(res.data.sysUser.id).then((res) => {
          const files = {}
          res.data.forEach((item) => {
            if (!files[item.moduleKey]) {
              files[item.moduleKey] = []
            }
            files[item.moduleKey].push({
              name: item.fileName,
              url: item.fullFilePath,
              id: item.id,
              filePath: item.filePath
            })
          })
          // if (files.signature && files.signature.length > 1) {
          //   files.signature.reverse()
          // }
          this.signatureFiles = files.signature || []
          // this.refreshStart = false;
        }).catch(e => {
          // this.refreshStart = false;
          this.$message.error(e.toString())
        })
      }
    }).catch((e) => {
      this.$message.error(e.toString());
    })
    this.getList();
  },
  methods: {
    getList(reload) {
      this.dataLoading = true;
      this.$api.zjk.expert
        .cgzhList(this.isReview ? '0' : '1', this.queryParams)
        .then((res) => {
          this.pagination.total = res.total;
          this.data = res.rows;
          this.dataLoading = false;
          if(reload){
            this.$emit("reload");
          }
        })
        .catch((e) => {
          this.$message.error(e.toString());
          this.dataLoading = false;
        });
    },
    handleReviewTeamDetail(row) {
      this.dialog.objId = row.reviewTeam.id;
      this.dialog.header = '评审组详情';
      this.dialog.open_review_group = true;
    },
    handleProjectDetail(row) {
      this.dialog.objId = row.collectResult.id;
      this.dialog.header = row.collectResult.name;
      this.dialog.open_detail = true
    },
    handleReview(row) {
      this.dialog.objId = row.id;
      this.dialog.teamId = row.teamId;
      this.dialog.header = row.collectResult.name;
      this.dialog.reviewType = row.reviewType;
      this.dialog.open_review = true
    },
    openReview(row){
      // 如果没有填写银行信息提示 完善信息
      if(!this.expertInfo?.expertBank?.bankName || this.signatureFiles.length<=0){
        this.visible = true;
        return false
      }
      this.dialog.row=row;
      this.dialog.header='专家评审';
      this.dialog.open_approve_info=true
    },
    onExpertInfoConfirm(context) {
      this.$router.push('/zjk/my-info');
    },
    onExpertInfoClose(){
      this.visible = false;
    }
  },
};
</script>

<style lang="less" scoped>
</style>
